package com.aruba.girlanime.dialog;

import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.aruba.girlanime.R;


/**
 * qr dialog
 * creator:aruba
 * time:2018.4.17
 */
public class QRDialog {
    private CustomDialog loadingdialog;
    private Context mContext;
    private ImageView iv_qr;
    private TextView tv_notice;

    public QRDialog(Context context) {
        init(context, -1);
    }

    public QRDialog(Context context, int ms) {
        init(context, ms);
    }

    private void init(Context context, int ms) {
        mContext = context;

        CustomDialog.Builder builder = new CustomDialog.Builder(mContext, R.layout.dialogwindow);
        loadingdialog = builder.create();

        iv_qr = (ImageView) loadingdialog.findViewById(R.id.iv_qr);

        loadingdialog.setCancelable(false);
        loadingdialog.setCanceledOnTouchOutside(false);
        //设置dialog居中显示
        Window dialogWindow = loadingdialog.getWindow();
        dialogWindow.setGravity(Gravity.TOP);

        if (ms > 0)
            runThread(ms);
    }

    private void runThread(final int ms) {
        new Thread() {
            public void run() {
                try {
                    this.sleep(ms);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(0);
            }

            ;
        }.start();
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            disMiss();
        }
    };

    public ImageView getQRImageView() {
        return iv_qr;
    }

    public TextView getNoticeView() {
        return tv_notice;
    }

    /**
     * 隐藏
     */
    public void disMiss() {
        if (loadingdialog != null && loadingdialog.isShowing())
            loadingdialog.dismiss();
    }

    public void show() {
        if (loadingdialog != null && !loadingdialog.isShowing()) {
            loadingdialog.show();
        }
    }

    public void showNotice() {
        if (tv_notice != null) {
            tv_notice.setVisibility(View.VISIBLE);
        }
    }
}
